package com.cz.zslu.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.cz.zslu.common.ReturnObject;
import com.cz.zslu.entity.*;
import com.cz.zslu.entity.HonorBook;
import com.cz.zslu.interceptor.BaseContextHandler;
import com.cz.zslu.interceptor.IgnoreAuth;
import com.cz.zslu.rest.BaseController;
import com.cz.zslu.service.CompanyBaseInfoService;
import com.cz.zslu.service.HonorBookService;
import com.cz.zslu.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 荣誉资质前端控制器
 * </p>
 *
 * @author Bruce
 * @since 2019-05-30
 */
@RestController
@RequestMapping("/sys/honorBook")
@Api(description = "荣誉资质管理")
public class HonorBookController extends BaseController<HonorBookService, HonorBook> {

    @Autowired
    private HonorBookService honorBookService;

    @Autowired
    private ISysUserService iSysUserService;


    @ApiOperation("获取详情")
    @IgnoreAuth
    @RequestMapping(value = "/findInfoById", method = RequestMethod.POST)
    public ReturnObject<HonorBook> findInfoById(@ApiParam("id") @RequestParam Long id) {

        return new ReturnObject<>(0, "获取成功", honorBookService.selectById(id));
    }

    @ApiOperation("新增/编辑（新增时：id为空）")
    @RequestMapping(value = "/saveBaseInfo", method = RequestMethod.POST)
    public ReturnObject<HonorBook> saveIntroduce(@ApiParam("名称") @RequestParam String name,
                                                 @ApiParam("图片") @RequestParam String honorPic,
                                                 @ApiParam("商铺id") @RequestParam Long storeId,
                                                 @ApiParam("id") @RequestParam(defaultValue = "0") Long id) {

        if (id != 0) {
            HonorBook info = honorBookService.selectById(id);
            info.setName(name);
            info.setHonorPic(honorPic);
            baseService.updateById(info);
        } else {
            HonorBook info = new HonorBook();
            info.setName(name);
            info.setHonorPic(honorPic);
            info.setStoreId(storeId);
            baseService.insert(info);
        }


        return new ReturnObject<>(0, "新增成功", null);
    }


    @ApiOperation("获取全部荣誉资质分页")
    @IgnoreAuth
    @RequestMapping(value = "/getHonorBookPage", method = RequestMethod.POST)
    public ReturnObject<List<HonorBook>> getAllNewPage(@ApiParam("每页条数") @RequestParam(defaultValue = "10") int limit,
                                                            @ApiParam("页数") @RequestParam(defaultValue = "1") int offset) {

        SysUser sysUser=iSysUserService.selectById(BaseContextHandler.getUserID());

        EntityWrapper<HonorBook> ew = new EntityWrapper<>();
        ew.eq("state", 0);
        ew.eq("store_id", sysUser.getStoreId());
        ew.orderBy("crt_time", false);
        Page<HonorBook> obj = baseService.selectPage(new Page<>(offset, limit), ew);

        return new ReturnObject<>(0, "获取成功", obj.getTotal(), obj.getPages(), obj.getRecords());
    }


    @ApiOperation("删除")
    @RequestMapping(value = "/del", method = RequestMethod.POST)
    public ReturnObject<HonorBook> del(@ApiParam("id") @RequestParam Long id) {

        HonorBook info = baseService.selectById(id);
        info.setState(1);
        baseService.updateById(info);

        return new ReturnObject<>(0, "删除成功", null);
    }

}

